/**
 * Controller class for the Address collection
 */
package ControlLayer;
import java.util.ArrayList;
import ModelLayer.*;
//model layer package has to be imported
public class AddressCtr
{
    private AddressBook ab;
    
    /**
     * Constructor for objects of class AddressCtr.
     */
    public AddressCtr()
    {
        ab = new AddressBook();
        
    }
    
    /**
     * This will return a person in a collection of Persons.
     */
    public Person getPerson(long id)
    {
        ArrayList<Person> temp = ab.getFriends();
        for(Person person : temp)
        {
            if (person.getId() == id)
            {
                return person;
            }
        }
        return null;
    }
    
    /**
     * This will print out the person.
     */
    public void printPerson(long id)
    {
        getPerson(id).printOut();
    }
    
    /**
     * This will create a person and add him in the collection of Persons.
     */
    public void createPerson(long id, String name, String address, String postalCode, String city, String phone)
    { 
       Person person = new Person(id, name, address, postalCode, city, phone);
       ab.getFriends().add(person);
       System.out.println("Person successfully added");

    }
    
    /**
     * This will delete a person from the collection of Persons.
     */
    public void deletePerson(long id)
    {
        ArrayList<Person> friendzone = ab.getFriends();
        int i = 0;
        boolean was_it = true;
        while(i < friendzone.size())
        {
            if (friendzone.get(i).getId() == id)
            {
                was_it = false;
                friendzone.remove(i);
                System.out.println("Person successfuly deleted");
                break;
            }
            i++;
        }
        if (was_it)
        {
            System.out.println("No such person");
        }
    }
    
    /**
     * This will update the information about a person.
     */
    public void updatePerson(long id, String name, String address, String postalCode, String city, String phone)
    {
        Person friend = getPerson(id);

        friend.setName(name);
        friend.setAddress(address);
        friend.setPostalCode(postalCode);
        friend.setCity(city);
        friend.setPhone(phone);
        System.out.println("Person data successfuly updated");        
    }
    
    /**
     * This will print out all the persons in the collection of Persons.
     */
    public void listAllPersons()
    {
          for (Person friend : ab.getFriends())
          {
              friend.printOut();
              System.out.println();
          }
    }
    
    /**
     * This will check if a certain person is in the collection of Persons.
     */
    public boolean isIt(long id)
    {
        if (getPerson(id) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
        
}
